package com.atguigu.lease.web.app.service.impl;

import com.atguigu.lease.common.context.LoginUserContext;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.model.enums.LeaseStatus;
import com.atguigu.lease.web.app.mapper.*;
import com.atguigu.lease.web.app.service.ApartmentInfoService;
import com.atguigu.lease.web.app.service.BrowsingHistoryService;
import com.atguigu.lease.web.app.service.RoomInfoService;
import com.atguigu.lease.web.app.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.app.vo.attr.AttrValueVo;
import com.atguigu.lease.web.app.vo.fee.FeeValueVo;
import com.atguigu.lease.web.app.vo.graph.GraphVo;
import com.atguigu.lease.web.app.vo.room.RoomDetailVo;
import com.atguigu.lease.web.app.vo.room.RoomItemVo;
import com.atguigu.lease.web.app.vo.room.RoomQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-26 11:12:39
 */
@Service
@Slf4j
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo> implements RoomInfoService {

    @Autowired
    private RoomInfoMapper roomInfoMapper;

    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;

    @Autowired
    private ApartmentInfoService apartmentInfoService;

    @Autowired
    private GraphInfoMapper graphInfoMapper;

    @Autowired
    private LabelInfoMapper labelInfoMapper;

    @Autowired
    private AttrValueMapper attrValueMapper;

    @Autowired
    private FacilityInfoMapper facilityInfoMapper;

    @Autowired
    private PaymentTypeMapper paymentTypeMapper;

    @Autowired
    private FeeValueMapper feeValueMapper;

    @Autowired
    private LeaseTermMapper leaseTermMapper;

    @Autowired
    private LeaseAgreementMapper leaseAgreementMapper;

    @Autowired
    private BrowsingHistoryService browsingHistoryService;

    @Override
    public IPage<RoomItemVo> pageItem(IPage<RoomItemVo> page, RoomQueryVo queryVo) {
        return roomInfoMapper.pageItem(page, queryVo);
    }

    @Override
    public RoomDetailVo getDetailById(Long id) {
        RoomDetailVo roomDetailVo = new RoomDetailVo();
        RoomInfo roomInfo = roomInfoMapper.selectById(id);
        if(roomInfo == null){
            return null;
        }
        BeanUtils.copyProperties(roomInfo, roomDetailVo);

        // 获取公寓信息
        ApartmentItemVo apartmentItemVo = apartmentInfoService.getApartmentItemVoByApartmentId(roomInfo.getApartmentId());
        Long apartmentId = apartmentItemVo.getId();
        roomDetailVo.setApartmentItemVo(apartmentItemVo);

        //获取图片列表
        roomDetailVo.setGraphVoList(graphInfoMapper.selectRoomGraphVoList(id));

        //获取属性信息列表
        List<AttrValueVo> attrValueVoList = attrValueMapper.selectAttrValueVoByRoomId(id);
        roomDetailVo.setAttrValueVoList(attrValueVoList);

        //获取配套信息列表
        List<FacilityInfo> facilityInfoList = facilityInfoMapper.selectFacilityInfoListByRoomId(id);
        roomDetailVo.setFacilityInfoList(facilityInfoList);

        //获取标签信息列表
        List<LabelInfo> labelInfoList = labelInfoMapper.selectRoomLabelInfoList(id);
        roomDetailVo.setLabelInfoList(labelInfoList);

        //获取支付方式列表
        List<PaymentType> paymentTypeList = paymentTypeMapper.listPaymentTypeByRoomId(id);
        roomDetailVo.setPaymentTypeList(paymentTypeList);

        //获取杂费列表
        List<FeeValueVo> feeValueVoList = feeValueMapper.selectFeeValueVoListByApartmentId(apartmentId);
        roomDetailVo.setFeeValueVoList(feeValueVoList);

        //获取租期列表
        List<LeaseTerm> leaseTermList = leaseTermMapper.selectLeaseTermListByRoomId(id);
        roomDetailVo.setLeaseTermList(leaseTermList);

        //获取房间状态
        LambdaQueryWrapper<LeaseAgreement> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(LeaseAgreement::getRoomId, id).in(LeaseAgreement::getStatus, LeaseStatus.SIGNED, LeaseStatus.WITHDRAWING);
        Long l = leaseAgreementMapper.selectCount(lambdaQueryWrapper);

        roomDetailVo.setIsDelete(roomInfo.getIsDeleted() == 1);
        roomDetailVo.setIsCheckIn(l > 0);

        Long userId = LoginUserContext.getLoginUser().getUserId();
        browsingHistoryService.saveHistory(userId,id);

        return roomDetailVo;
    }

    @Override
    public IPage<RoomItemVo> pageRoomItemVoByApartmentId(Page<RoomItemVo> page, Long id) {
        return roomInfoMapper.pageRoomItemVoByApartmentId(page, id);
    }
}




